// pages/login/login.js
let CONSTANT = require('../../common/constant.js');
let util = require('../../utils/util.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timeCountDown:"获取验证码",
    counting:false,
    tel:"",
    pw:""
  },
  bindInput:function(e){
    this.setData({
      [e.currentTarget.dataset.id]:e.detail.value
    })
  },
  bindGetCode:function(){
    if(this.data.timeCountDown!='获取验证码'){return};
    var that = this
    if(that.data.tel==""){
      app.titleToast("请输入手机号")
      return
    }
    wx.request({
      url: 'https://groomtea.icebartech.com/api/miniApp/salesman/findMobileCode', 
      method:"POST",
      data: {
        "bizType": 'login',
        "mobile": that.data.tel
      },
      success(res) {
        if (res.data.status==200){
          console.log(res)
          //重试倒计时
          // that.setData({
          //   pw: res.data.data.bussData.bizType
          // })
          if (!that.data.counting) {
            app.titleToast("验证码已发送")
            that.countDown(that,60)   
          }
        }
        
        else {
          console.log(res.data)
          app.titleToast(res)
        }
        
      },
      fail(res){
        app.titleToast(res)
      }
    })
    
  },
  countDown:function(that,count) {
    if (count == 0) {
      that.setData({
        timeCountDown: '获取验证码',
        counting:false
      })
      return;
    }
  
    that.setData({
      counting:true,
      timeCountDown: count + '秒后重新获取',
    })
    
    setTimeout(function(){
      count--;
      that.countDown(that, count);
    }, 1000);
  },
  bindSubmit:function(){
    if (this.data.tel==""){
      app.titleToast("请输入手机号")
      return
    }
    if (this.data.pw == "") {
      app.titleToast("请输入验证码")
      return
    }
    var that = this
    console.log(wx.getStorageSync("openId"))
    wx.request({
      url: "https://groomtea.icebartech.com/api/miniApp/salesman/login",
      header:{
        "Accept": "*/*"
      },
      data: {
        "account": that.data.tel,
        "code": that.data.pw,
        "openId": wx.getStorageSync("openId")
      },
      method: 'POST',
      success:(res)=>{
        console.log('登录login返回',res)
        if(res.data.status == 500){
          app.titleToast(res.data.msg)
          return;
        }
        app.globalData.userInfo = res.data.data.bussData;
        wx.setStorageSync("userInfo", res.data.data.bussData)
        wx.switchTab({
          url: '../index/index',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    util.login();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})